package com.role;

import java.util.ArrayList;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.controller.Direction;
import com.map.GameMap;

public class RolesCache {
	public static String RoleInitData[]=
	{
			"data/actor/actor1.png	data/actor/actor4.png	data/actor/fight1.png	400	100	MAIN	1",
			"data/actor/monster_406_walk.png	data/actor/actor4.png	data/actor/monster_406_fight.png	300	180	MONSTER	1",
			"data/actor/actor1.png	data/actor/actor4.png	data/actor/fight1.png	400	100	MAIN	2",
	};
	private ArrayList<RoleInfo> roleinfos;
	public RolesCache(String[] rids,GameMap gmap)
	{
		this.ResetCache(rids,gmap);
	}
	public void ResetCache(String[] rids,GameMap gmap)
	{
		if(roleinfos==null){
			roleinfos=new ArrayList<RoleInfo>();
		}
		else
		{
			roleinfos.clear();
		}
		for(String rid :rids){
			RoleInitData ridata = new RoleInitData(rid);
			if(ridata.map_index == gmap.map_index){
				roleinfos.add(new RoleInfo(ridata));
			}
		}
	}
	public ArrayList<RoleInfo> detectDamageUnit(RoleInfo mrole)
	{
		ArrayList<RoleInfo> rifs = new ArrayList<RoleInfo>();
		
		return rifs;
	}
	public RoleInfo getMainRole()
	{
		RoleInfo res = null;
		for(RoleInfo monster:roleinfos){
			if(monster.roleType==RoleType.MAIN){
				res=monster;
				break;
			}
		}
		return res;
	}
	public int cmp(RoleInfo r1,RoleInfo r2)
	{
		return (int)(r1.getViewY()-r2.getViewY());
	}
	public void sortRoles()
	{
		for(int i=0 ;i<roleinfos.size()-1;i++)
		{
			for(int j=i+1;j<roleinfos.size();j++)
			{
				if(cmp(roleinfos.get(i),roleinfos.get(j))<0)
				{
					RoleInfo  tmp;
					tmp = roleinfos.get(i);
					roleinfos.set(i,roleinfos.get(j));
					roleinfos.set(j, tmp);
				}
			}
		}
	}
	public void setMonsterPosition(int direction, int distance)
	{
		int dis_x=0;
		int dis_y=0;
		switch(direction)
		{
			case Direction.left: dis_x=distance;break;
			case Direction.right:dis_x=-distance;break;
			case Direction.up:dis_y=-distance;break;
			case Direction.down:dis_y=distance;break;
		}
		for(RoleInfo monster:roleinfos){
			if(monster.roleType==RoleType.MONSTER){
				float msx = monster.getSprite().getX();
				float msy = monster.getSprite().getY();
				monster.getSprite().setPosition(msx + dis_x, msy+dis_y);
			}
		}
	}
	public Boolean isBlock(RoleInfo mrole)
	{
		boolean res = false;
		for(RoleInfo monster:roleinfos){
			if(monster.roleType!=RoleType.MAIN&&monster.isBlock(mrole)){
				res=true;
				break;
			}
		}
		return res;
	}
	public void drawRoleinfos(SpriteBatch batch)
	{
		this.sortRoles();
		for(RoleInfo monster:roleinfos){
			monster.drawSprite(batch);
		}
	}
	public void dectectAnimy(RoleInfo mrole){
		for(RoleInfo monster:roleinfos){
			if(monster.roleType==RoleType.MONSTER)
				monster.dectectAnimy(mrole);
		}
	}
	public void setBlood()
	{
		for(RoleInfo monster:roleinfos){
			monster.setBlood();
		}
	}
	public ArrayList<RoleInfo> getroleinfos() {
		return roleinfos;
	}

	public void setroleinfos(ArrayList<RoleInfo> roleinfos) {
		this.roleinfos = roleinfos;
	}
	
}
